<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('activity.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('activity.title')" prop="title">
            <el-input v-model="dataForm.title" :activityholder="i18n('common.please_input') + i18n('activity.title')" clearable></el-input>
          </el-form-item>

          <el-form-item :label="i18n('activity.contact_id')" prop="contact_id">
            <el-select v-model="dataForm.contact_id" :activityholder="i18n('common.please_select') + i18n('activity.contact_id')" clearable>
              <el-option v-for="(v, k) in contactList" :label="v.name" :key="k" :value="v.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item class="mavon" prop="content" :label="i18n('activity.content')">
            <editor ref="editorRef" :content="dataForm.content"></editor>
          </el-form-item>

          <el-form-item :label="i18n('common.is_hidden')" prop="is_hidden">
            <el-switch
              v-model="dataForm.is_hidden"
              active-value="1"
              :active-text="i18n('common.yes')"
              inactive-value="2"
              :inactive-text="i18n('common.no')">
            </el-switch>
          </el-form-item>

          <el-form-item :label="i18n('common.sort')" prop="sort">
            <el-input-number v-model="dataForm.sort" :min="0">
            </el-input-number>
          </el-form-item>

          <el-form-item>
            <el-button
              v-if="isAuth('module:business:activity:handle')"
              type="primary"
              @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>

            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="business-activity-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useStore } from "@/hooks/store/useStore";
  import Editor from '@/components/form/editorjs/editorjs'

  const { router, query, i18n, isAuth } = useBase()
  const { user } = useStore()

  const model = ref('business/activity')

  const editorRef = ref()
  const contactList = ref([])

  const dataForm = reactive({
    id: 0,
    contact_id: '',
    title: '',
    content: '',
    sort: 0,
    is_hidden: '2',
  })

  const dataRule = reactive({
    contact_id: [{
      required: true,
      message: i18n('activity.rules.contact_id.require'),
      trigger: 'blur'
    }],
    title: [{
      required: true,
      message: i18n('activity.rules.title.require'),
      trigger: 'blur'
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.contact_id = data.data.contact_id
            dataForm.title = data.data.title
            dataForm.content = data.data.content
            dataForm.is_hidden = data.data.is_hidden.value + ''
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate((valid) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            contact_id: dataForm.contact_id,
            title: dataForm.title,
            content: editorRef.value.data,
            is_hidden: dataForm.is_hidden,
            sort: dataForm.sort,
            status: dataForm.status,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  const loadContactList = () => {
    $http({
      url: $http.adornUrl('/business/contact/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        contactList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    init(formRef)

    loadContactList()
  })
</script>
